#include "sgl_draw.h"
#include "stdio.h"

#if (SGL_CONFIG_DRAW_ALGO == 0)

uint16_t sgl_render_circle_index[SGL_RENDER_CIRCLE_MAX_SIZE] = {
0,1,3,6,10,15,15,20,26,33,41,49,58,68,78,89,101,114,127,141,156,171,187,204,222,240,259,279,299,320,342,364,387,411,436,461,487,514,541,569,598,628,658,689,721,753,786,820,854,889,925,962,999,1037,1076,1115,1155,1196,1238,1280,1323,1367,1411,1456,1502,1549,1596,1644,1693,1742,1792,1843,1894,1946,1999,2053,2107,2162,2218,2274,2331,2389,2448,2507,2567,2628,2689,2751,2814,2877,2941,3006,3072,3138,3205,3273,3341,3410,3480,3551,3622,
};

uint8_t sgl_render_circle_tab[SGL_RENDER_CIRCLE_TAB_SIZE] = {
0,
0,0,
85,128,128,
96,192,192,128,
102, 25, 76, 76,128,
106, 42,149,170,128,
109, 54,182, 54, 73,128,
112, 64,208, 96,128,128,128,
113, 71,227,128,199, 28,199,128,
115, 76, 12,153, 38,102,115,128,
116, 81, 23,174, 69,151,186,186,128,
117, 85, 32,192, 96,192, 53, 74, 74,128,
118, 88, 39,206,118,  9, 98,147,157,128,
118, 91, 45,219,137, 36,137,201, 45, 54,128,
119, 93, 51,230,153, 59,170, 42,102,119,119,128,
120, 96, 56,240,168, 80,200, 80,152,192, 16,192,128,
120, 97, 60,  7,180, 97,225,112,195, 52, 90, 97,128,
120, 99, 64, 14,192,113, 21,142, 21, 99,149,163,163,128,
121,101, 67, 20,202,128, 40,168, 53,141,202, 47, 60, 60,128,
121,102, 70, 25,211,140, 57,192, 83,179, 44,102,134,140,128,
121,103, 73, 30,219,152, 73,213,109,213, 85,152,195, 30, 36,128,
122,104, 75, 34,226,162, 87,232,133, 23,122,197, 52, 93,104,104,128,
122,105, 77, 38,233,172,100, 16,155, 50,155, 27,100,150,178,178,150,128,
122,106, 80, 42,240,181,112, 32,176, 74,186, 64,144,202, 48, 74, 80,128,
122,107, 81, 46,245,189,122, 46,194, 97,215, 97,184, 46,102,138,148,148,128,
123,108, 83, 49,  4,196,132, 59,211,118, 14,128,  4, 88,152,196, 34, 44, 44,128,
123,109, 85, 52,  9,203,142, 71,227,137, 37,156, 37,128,199, 52, 94,118,123,128,
123,109, 86, 54, 13,210,150, 82,  4,155, 59,182, 68,164, 32,100,150,182, 13, 18,128,
123,110, 88, 57, 17,216,158, 92, 17,172, 79,207, 97,198, 70,145,203, 48, 79, 88, 88,128,
123,110, 89, 59, 21,221,166,102, 29,187, 98,230,123,  8,106,187, 46,102,140,162,166,128,
123,111, 90, 61, 24,227,173,111, 41,202,115, 20,148, 37,140, 12, 90,152,198, 37, 57, 61,128,
124,112, 92, 64, 28,232,180,120, 52,216,132, 40,172, 64,172, 48,132,200, 52, 96,124,132,132,128,
124,112, 93, 65, 31,236,186,128, 62,228,147, 58,193, 89,201, 81,170, 34,100,151,186, 19, 27, 27,128,
124,112, 94, 67, 33,240,192,135, 71,240,161, 75,214,112,  3,112,207, 75,146,203, 48, 82,101,105,128,
124,113, 95, 69, 36,245,197,142, 80, 10,175, 91,234,135, 29,142, 21,113,190, 47,102,142,168,175,175,128,
124,113, 96, 71, 39,248,202,149, 88, 21,188,106, 17,156, 53,170, 53,149, 17, 92,152,199, 39, 64, 71, 71,128,
124,114, 96, 72, 41,  3,207,155, 96, 31,200,121, 34,176, 76,197, 83,183, 55,134,200, 51, 96,128,145,148,128,
124,114, 97, 74, 43,  6,212,161,104, 40,212,134, 50,195, 97,222,111,215, 90,175, 37,101,151,188, 23, 40, 43,128,
124,114, 98, 75, 45,  9,216,167,111, 49,223,147, 65,213,118, 16,137, 22,124,213, 78,147,203, 49, 85,108,114,114,128,
124,115, 99, 76, 48, 12,220,172,118, 57,233,160, 80,230,137, 38,163, 51,156, 32,118,192, 48,102,144,172,  3,  9,  9,128,
124,115, 99, 78, 49, 15,224,177,124, 65,243,171, 93,  9,156, 59,187, 78,187, 65,156, 21, 93,152,199, 40, 68, 84, 87,128,
124,115,100, 79, 51, 18,228,182,131, 73,  9,182,106, 24,173, 79,210,103,216, 97,192, 60,137,201, 51, 97,131,152,158,158,128,
125,116,101, 80, 53, 20,232,187,136, 80, 17,193,119, 38,190, 98,232,128, 17,128,  5, 98,178, 38,101,151,190, 26, 47, 53, 53,128,
125,116,101, 81, 55, 23,235,192,142, 87, 26,203,130, 52,206,116, 20,151, 43,157, 37,133,  2, 81,148,203, 49, 87,113,128,130,128,
125,116,102, 82, 56, 25,238,196,147, 93, 34,213,142, 65,221,133, 39,173, 68,184, 68,167, 39,122,193, 48,102,145,176,  8, 22, 25,128,
125,116,102, 83, 58, 27,242,200,153,100, 41,222,153, 77,236,150, 58,194, 91,211, 97,200, 75,161, 25, 94,153,200, 41, 72, 91, 97, 97,128,
125,117,103, 84, 59, 29,245,204,157,106, 49,231,163, 89, 10,166, 76,215,114,  8,125,  8,108,198, 65,138,201, 51, 98,133,157,171,174,128,
125,117,104, 85, 61, 32,248,208,162,112, 56,240,173,101, 24,181, 93,234,136, 32,152, 37,141, 16,104,181, 40,101,152,192, 29, 53, 66, 69,128,
125,117,104, 86, 62, 33,250,211,167,117, 62,  2,182,112, 36,195,109, 18,156, 54,177, 65,172, 49,141,  7, 83,148,203, 49, 88,117,135,141,141,128,
125,117,104, 87, 64, 35,  2,215,171,122, 69, 10,192,122, 48,209,125, 35,176, 76,202, 92,202, 81,176, 46,125,194, 48,102,145,179, 12, 30, 35, 35,128,
125,117,105, 87, 65, 37,  5,218,175,128, 75, 17,200,133, 60,223,140, 52,195, 97,225,117,  5,112,210, 82,165, 27, 95,153,200, 42, 75, 97,110,112,128,
125,118,105, 88, 66, 39,  7,221,179,132, 81, 24,209,142, 71,236,155, 68,214,118, 17,142, 32,142, 22,118,204, 68,140,201, 51, 98,135,162,179,  4,  7,128,
125,118,106, 89, 67, 41,  9,224,183,137, 86, 31,217,152, 82,  7,169, 84,231,137, 38,166, 57,171, 53,152, 24,108,183, 41,101,152,193, 31, 57, 74, 79, 79,128,
125,118,106, 90, 68, 42, 11,227,187,142, 92, 37,225,161, 92, 18,182, 99, 11,156, 59,189, 82,199, 82,184, 59,146, 11, 85,149,203, 49, 90,120,142,154,156,128,
125,118,107, 90, 69, 44, 13,230,190,146, 97, 44,232,169,102, 30,195,114, 27,174, 79,211,107,225,111,216, 93,183, 51,128,195, 48,102,146,181, 16, 37, 48, 51,128,
125,118,107, 91, 70, 45, 16,233,194,150,102, 50,240,178,112, 41,208,128, 43,192, 98,233,130, 22,139, 22,125,219, 89,169, 29, 96,153,201, 43, 77,102,118,123,123,128,
125,119,107, 92, 71, 47, 17,235,197,154,107, 56,247,186,121, 51,220,141, 58,208,116, 20,152, 47,166, 51,157, 33,125,208, 71,141,202, 51, 98,136,166,186, 13, 17, 17,128,
125,119,108, 92, 72, 48, 19,238,200,158,112, 61,  6,194,130, 61,231,154, 72,225,134, 39,174, 70,192, 79,187, 66,161, 30,112,185, 41,101,152,194, 33, 61, 81, 92, 94,128,
125,119,108, 93, 73, 49, 21,240,203,162,117, 67, 13,201,138, 71,242,167, 86,  2,151, 58,195, 93,216,106,216, 97,195, 67,151, 15, 86,149,203, 49, 91,123,147,162,167,167,128,
125,119,108, 93, 74, 51, 23,243,206,166,121, 72, 19,209,147, 81, 10,179,100, 17,168, 76,215,115, 10,132, 19,128,  6,102,189, 55,130,196, 49,102,147,183, 19, 42, 57, 61, 61,128,
125,119,109, 94, 75, 52, 25,245,209,169,125, 77, 25,216,155, 90, 20,190,113, 31,184, 94,235,136, 33,157, 46,157, 37,136,  8, 94,172, 31, 96,153,201, 44, 79,107,125,136,138,128,
125,119,109, 94, 76, 53, 26,247,212,173,130, 82, 30,222,163, 99, 30,202,125, 45,200,111, 18,156, 55,181, 72,185, 68,169, 43,132,212, 74,142,202, 51, 99,138,169,  2, 20, 30, 33,128,
125,119,109, 95, 77, 54, 28,249,215,176,134, 87, 36,229,170,107, 40,213,138, 58,215,128, 36,176, 77,205, 97,213, 97,201, 77,168, 36,115,186, 42,101,152,195, 34, 65, 87,101,105,105,128,
126,120,110, 96, 78, 56, 30,252,218,180,138, 92, 42,236,178,116, 50,224,150, 72,230,144, 54,196, 98,228,122, 12,126,  8,110,204, 74,156, 18, 88,150,204, 50, 92,126,152,170,180,180,170,128,
126,120,110, 96, 78, 57, 31,  1,220,183,141, 96, 47,242,185,124, 59,234,161, 84,  3,159, 70,214,118, 17,145, 37,153, 37,141, 17,110,194, 59,131,196, 49,102,147,185, 21, 47, 64, 74, 76,128,
126,120,110, 96, 79, 58, 32,  3,223,186,145,100, 52,248,192,131, 67,244,172, 96, 17,174, 87,232,137, 38,168, 62,180, 65,172, 50,145, 15, 98,174, 32, 96,153,201, 44, 81,110,131,145,149,149,128,
126,120,110, 97, 80, 59, 34,  5,225,189,149,105, 57,  5,198,139, 76,  9,183,108, 30,189,103, 13,156, 59,191, 85,206, 93,202, 82,179, 51,137,  1, 76,143,202, 51, 99,139,171,  5, 26, 40, 43, 43,128,
126,120,111, 97, 80, 60, 35,  7,227,192,152,109, 62, 11,205,146, 84, 18,193,120, 43,203,118, 30,175, 79,212,109,  1,120,  5,112,212, 86,175, 41,118,188, 43,101,152,195, 35, 67, 92,109,118,120,128,
126,120,111, 98, 81, 61, 37,  9,230,194,155,113, 66, 16,211,153, 92, 27,204,131, 55,217,133, 46,192, 98,233,131, 25,146, 33,142, 22,120,211, 79,159, 20, 89,150,204, 50, 92,128,155,176,  3, 12, 14,128,
126,120,111, 98, 82, 62, 38, 10,232,197,159,117, 71, 21,217,160,100, 36,213,142, 67,230,148, 62,210,117, 20,153, 49,171, 60,171, 53,153, 27,117,199, 62,133,197, 49,102,148,186, 23, 51, 71, 84, 87, 87,128,
126,120,111, 99, 82, 63, 39, 12,234,200,162,120, 75, 27,223,167,108, 45,223,153, 79,  1,162, 77,227,135, 39,174, 72,196, 86,200, 82,185, 61,153, 21,102,176, 34, 97,153,201, 45, 82,113,137,153,162,164,128,
126,120,112, 99, 83, 64, 40, 14,236,202,165,124, 80, 32,229,174,115, 53,232,163, 90, 14,176, 92,  5,152, 58,195, 94,220,112,227,112,216, 94,188, 58,142,  5, 78,144,202, 51, 99,140,174,  8, 32, 48, 56, 58,128,
126,120,112, 99, 84, 64, 42, 15,238,205,168,128, 84, 36,234,180,122, 61,241,173,101, 26,189,106, 21,170, 77,215,115, 12,136, 26,140, 22,126,  1, 94,180, 45,120,189, 43,101,152,196, 36, 70, 96,115,128,131,131,128,
126,121,112,100, 84, 65, 43, 17,240,207,171,131, 88, 41,240,186,129, 69,  5,183,112, 38,202,121, 36,186, 95,235,136, 34,160, 51,167, 51,157, 34,129,217, 84,162, 22, 89,150,204, 50, 93,129,159,181, 10, 22, 25, 25,128,
126,121,112,100, 85, 66, 44, 18,242,209,174,134, 92, 46,245,192,136, 76, 13,192,122, 49,215,134, 51,203,112, 18,157, 56,184, 76,194, 80,187, 66,163, 35,122,203, 64,134,197, 49,102,148,187, 25, 54, 76, 92,100,102,128,
126,121,112,101, 85, 67, 45, 20,244,212,176,138, 96, 50,  1,198,143, 84, 21,202,133, 60,227,148, 65,218,129, 37,176, 77,207,101,220,107,217, 97,197, 70,160, 26,106,178, 35, 97,153,202, 45, 84,116,141,160,171,175,175,128,
126,121,113,101, 86, 68, 46, 21,246,214,179,141, 99, 54,  6,204,149, 91, 29,211,142, 71,239,161, 79,234,146, 54,196, 98,229,124, 16,134, 19,128,  6,104,196, 64,146,  8, 79,144,202, 51, 99,141,176, 11, 36, 54, 66, 69, 69,128,
126,121,113,101, 86, 68, 47, 22,247,216,182,144,103, 59, 11,210,155, 98, 37,219,152, 82,  8,173, 93,  9,162, 72,214,118, 18,147, 41,160, 47,157, 37,137, 11,101,185, 49,123,190, 44,101,152,196, 37, 72,100,121,136,144,146,128,
126,121,113,102, 87, 69, 48, 24,249,218,184,147,106, 63, 16,215,162,105, 45,228,162, 92, 19,186,106, 24,178, 89,233,137, 38,170, 64,186, 74,186, 68,170, 45,137,  6, 89,165, 24, 90,150,204, 50, 93,131,162,186, 16, 30, 38, 40,128,
126,121,113,102, 88, 70, 49, 25,251,220,187,150,110, 67, 20,220,168,112, 52,236,171,102, 30,198,120, 38,193,105, 14,156, 59,192, 88,211,100,214, 97,201, 78,172, 43,128,206, 67,136,198, 49,102,148,188, 27, 57, 81, 99,110,113,113,128,
126,121,113,102, 88, 71, 50, 26,252,222,189,153,113, 71, 25,225,173,118, 60,244,180,112, 41,210,132, 52,208,121, 31,175, 79,213,110,  4,126, 14,126,  7,110,207, 79,165, 31,109,180, 36, 97,153,202, 45, 85,118,145,165,180,  4,  7,  7,128,
126,121,113,103, 88, 71, 51, 28,  1,224,192,156,117, 74, 29,231,179,124, 67,  6,188,121, 51,221,145, 65,223,137, 48,193, 98,234,132, 28,151, 40,154, 37,142, 18,113,202, 70,149, 10, 81,145,202, 51, 99,142,177, 14, 40, 60, 74, 82, 84,128,
126,121,114,103, 89, 72, 52, 29,  3,226,194,158,120, 78, 33,235,185,131, 74, 13,197,131, 61,232,157, 78,237,152, 64,211,117, 20,154, 50,175, 66,181, 66,172, 50,148, 20,107,189, 52,124,191, 44,101,152,197, 38, 74,103,126,143,154,157,157,128,
126,121,114,103, 89, 73, 53, 30,  4,228,196,161,123, 82, 38,240,190,137, 80, 21,205,140, 71,243,169, 91, 10,167, 80,228,135, 39,175, 73,199, 91,208, 94,202, 82,181, 54,144, 12, 92,167, 25, 91,150,204, 50, 94,132,164,190, 21, 38, 48, 51, 51,128,
126,121,114,103, 90, 73, 54, 31,  6,230,198,164,126, 85, 42,245,195,143, 87, 28,213,149, 81, 10,180,103, 24,182, 96,  7,153, 58,195, 94,222,115,  6,121,  6,112,213, 88,180, 49,132,209, 69,137,198, 49,102,149,189, 28, 60, 85,105,118,126,128,128,
126,122,114,104, 90, 74, 55, 32,  7,232,200,166,129, 89, 46,250,200,148, 93, 35,221,157, 90, 20,192,116, 37,196,111, 23,171, 77,215,116, 13,139, 31,148, 34,142, 22,122,215, 86,171, 35,111,181, 37, 98,153,202, 46, 86,120,148,171,187, 13, 20, 22,128,
126,122,114,104, 91, 75, 55, 33,  8,233,203,169,132, 92, 50,  4,205,154,100, 42,229,166,100, 30,203,128, 50,210,126, 39,188, 95,235,136, 35,163, 55,175, 61,172, 52,154, 29,122,208, 75,153, 13, 82,145,203, 51,100,142,179, 16, 44, 66, 82, 92, 95, 95,128,
126,122,114,104, 91, 75, 56, 34, 10,235,205,171,135, 96, 53,  8,210,160,106, 49,237,174,109, 40,213,139, 62,224,141, 55,205,113, 18,157, 56,186, 80,200, 88,200, 82,186, 62,157, 27,113,193, 55,126,192, 45,101,152,197, 39, 75,106,130,149,162,170,171,128,
126,122,115,104, 92, 76, 57, 35, 11,237,207,174,138, 99, 57, 12,215,165,112, 56,244,182,117, 50,224,151, 74,237,155, 70,221,130, 37,176, 77,208,103,225,115,  1,112,217, 94,191, 63,151, 17, 96,169, 27, 92,151,204, 50, 94,133,166,  1, 25, 44, 57, 64, 66,128,
126,122,115,105, 92, 76, 58, 36, 12,238,209,176,140,102, 61, 17,220,170,118, 62,  4,190,126, 59,234,162, 86,  8,169, 85,237,147, 55,196, 98,230,126, 19,140, 28,140, 22,126,  2, 98,187, 55,136,211, 71,137,199, 49,102,149,190, 29, 62, 89,110,126,136,139,139,128,
126,122,115,105, 92, 77, 59, 37, 14,240,210,178,143,105, 64, 21,225,175,123, 68, 11,198,135, 68,244,173, 98, 21,182, 99, 14,164, 73,215,118, 18,149, 43,165, 54,168, 52,157, 35,132,  4, 92,175, 39,113,182, 37, 98,153,202, 46, 87,122,151,175,  5, 21, 30, 33, 33,128,
126,122,115,105, 93, 77, 59, 38, 15,242,212,180,146,108, 68, 25,229,180,129, 75, 18,205,143, 77,  9,183,109, 33,196,114, 29,180, 90,233,137, 38,171, 66,190, 80,196, 80,187, 66,165, 38,129,214, 79,155, 15, 83,146,203, 51,100,143,180, 18, 47, 70, 89,101,108,109,128,
126,122,115,105, 93, 78, 60, 39, 16,243,214,183,148,111, 71, 28,233,185,134, 81, 24,213,151, 86, 19,194,121, 45,209,128, 44,196,107, 15,156, 59,192, 89,214,105,222,108,217, 97,198, 72,165, 34,118,196, 57,128,192, 45,101,152,198, 39, 77,108,134,155,170,180,  2,  4,128,
126,122,115,106, 93, 78, 61, 40, 17,245,216,185,151,114, 74, 32,238,190,140, 87, 31,220,159, 95, 28,204,132, 57,221,141, 58,212,123, 32,175, 78,213,111,  6,130, 20,136, 20,128,  6,106,200, 70,156, 21, 99,171, 28, 92,151,204, 50, 95,134,168,  4, 29, 50, 65, 74, 77, 77,128,
126,122,115,106, 94, 79, 61, 41, 18,246,218,187,153,117, 78, 36,242,195,145, 92, 37,227,167,103, 37,214,142, 68,234,154, 72,227,140, 49,194, 98,234,133, 29,154, 45,163, 48,157, 37,138, 13,106,194, 60,140,  1, 72,138,199, 49,102,149,191, 30, 64, 92,115,133,145,152,153,128,
126,122,116,106, 94, 80, 62, 42, 20,248,220,189,156,120, 81, 40,246,200,150, 98, 44,234,174,112, 46,224,153, 80,  4,168, 86,  2,156, 66,212,117, 20,154, 52,178, 70,189, 76,186, 68,170, 46,141, 12, 98,180, 42,116,184, 38, 98,153,202, 46, 88,124,154,180, 10, 28, 40, 46, 48,128,
126,122,116,106, 95, 80, 63, 43, 21,249,221,191,158,122, 84, 43,250,204,155,104, 50,241,182,120, 55,233,163, 91, 15,180,100, 17,171, 83,229,135, 39,175, 73,201, 95,215,102,215, 97,201, 79,175, 47,135,  2, 83,158, 17, 84,146,203, 51,100,143,182, 19, 50, 75, 95,109,118,121,121,128,
126,122,116,107, 95, 80, 64, 44, 22,250,223,193,160,125, 87, 47,  3,208,160,109, 56,248,189,128, 64,242,173,101, 27,193,113, 31,186, 99,  9,154, 58,195, 95,224,118, 10,129, 15,126,  7,111,208, 82,172, 40,122,199, 60,129,193, 45,101,152,198, 40, 78,111,138,160,177,  3, 13, 15, 15,128,
126,122,116,107, 95, 81, 64, 45, 23,252,224,195,162,128, 90, 50,  7,213,165,115, 62,  6,196,135, 72,  6,183,112, 38,205,126, 45,201,115, 25,171, 77,215,116, 14,142, 34,155, 42,155, 37,142, 19,116,208, 77,161, 25,102,173, 29, 93,151,204, 50, 95,135,170,  6, 33, 55, 72, 84, 90, 91,128,

};

#endif

#ifdef SGL_FULL_FRAMEBUFFER
extern sgl_color_t frame_buffer[SGL_CONFIG_PANEL_YRES * SGL_CONFIG_PANEL_XRES];
#else
sgl_color_t sgl_draw_buffer[SGL_DRAW_BUFFER_SIZE];
#endif

#if (SGL_CONFIG_MIXER_TYPE == 1)

sgl_color_t sgl_color_mixer(sgl_color_t fg_color, sgl_color_t bg_color, uint8_t factor)
{
    sgl_color_t ret;;
    //RGB888 or ARGB8888
#if (SGL_CONFIG_PANEL_PIXEL_DEPTH != 1)
#if (SGL_CONFIG_PANEL_PIXEL_DEPTH == 32)
    #define __MIXER_BIT_R  0
    #define __MIXER_BIT_G  8
    #define __MIXER_BIT_B  16
    #define __MIXER_BIT_R_LEN 8
    #define __MIXER_BIT_G_LEN 8
    #define __MIXER_BIT_B_LEN 8
    //RGB565
#elif(SGL_CONFIG_PANEL_PIXEL_DEPTH == 16) 
    #define __MIXER_BIT_R  0
    #define __MIXER_BIT_G  5
    #define __MIXER_BIT_B  11
    #define __MIXER_BIT_R_LEN 5
    #define __MIXER_BIT_G_LEN 6
    #define __MIXER_BIT_B_LEN 5
#else
    //add your pixel bit type
#endif
    ret.red = (fg_color.red * factor + bg_color.red * (256 - factor)) >> 8;
    ret.green = (fg_color.green * factor + bg_color.green * (256 - factor)) >> 8;
    ret.blue = (fg_color.blue * factor + bg_color.blue * (256 - factor)) >> 8;
    return ret;
#else
    return 0;
#endif
}

#else

sgl_color_t sgl_color_mixer(int fg_color, int bg_color, uint8_t factor)
{
    int f_r, f_g, f_b, b_r, b_g, b_b, r, g, b;
    //RGB888 or ARGB8888
#if (SGL_CONFIG_PANEL_PIXEL_DEPTH != 1)
#if (SGL_CONFIG_PANEL_PIXEL_DEPTH == 32 || SGL_CONFIG_PANEL_PIXEL_DEPTH == 24) 
    #define __MIXER_BIT_R  0
    #define __MIXER_BIT_G  8
    #define __MIXER_BIT_B  16
    #define __MIXER_BIT_R_LEN 8
    #define __MIXER_BIT_G_LEN 8
    #define __MIXER_BIT_B_LEN 8
    //RGB565
#elif(SGL_CONFIG_PANEL_PIXEL_DEPTH == 16) 
    #define __MIXER_BIT_R  0
    #define __MIXER_BIT_G  5
    #define __MIXER_BIT_B  11
    #define __MIXER_BIT_R_LEN 5
    #define __MIXER_BIT_G_LEN 6
    #define __MIXER_BIT_B_LEN 5
#else 
    //add your pixel bit type
#endif
    f_r = (fg_color >> __MIXER_BIT_R) & ((1<<__MIXER_BIT_R_LEN) - 1);
    f_g = (fg_color >> __MIXER_BIT_G) & ((1<<__MIXER_BIT_G_LEN) - 1);
    f_b = (fg_color >> __MIXER_BIT_B) & ((1<<__MIXER_BIT_B_LEN) - 1);
    b_r = (bg_color >> __MIXER_BIT_R) & ((1<<__MIXER_BIT_R_LEN) - 1);
    b_g = (bg_color >> __MIXER_BIT_G) & ((1<<__MIXER_BIT_G_LEN) - 1);
    b_b = (bg_color >> __MIXER_BIT_B) & ((1<<__MIXER_BIT_B_LEN) - 1);
    r = (f_r * factor + b_r*(255 - factor)) >> 8;
    g = (f_g * factor + b_g*(255 - factor)) >> 8;
    b = (f_b * factor + b_b*(255 - factor)) >> 8;
    return (r<<__MIXER_BIT_R) + (g <<__MIXER_BIT_G) + (b<<__MIXER_BIT_B);
#else 
    return 0;
#endif
}

#endif

// TODO
/*
// Fast RGB565 pixel blending
// Found in a pull request for the Adafruit framebuffer library. Clever!
// https://github.com/tricorderproject/arducordermini/pull/1/files#diff-d22a481ade4dbb4e41acc4d7c77f683d
color alphaBlendRGB565( uint32_t fg, uint32_t bg, uint8_t alpha ){
    // Alpha converted from [0..255] to [0..31]
    alpha = ( alpha + 4 ) >> 3;

    // Converts  0000000000000000rrrrrggggggbbbbb
    //     into  00000gggggg00000rrrrr000000bbbbb
    // with mask 00000111111000001111100000011111
    // This is useful because it makes space for a parallel fixed-point multiply
    bg = (bg | (bg << 16)) & 0b00000111111000001111100000011111;
    fg = (fg | (fg << 16)) & 0b00000111111000001111100000011111;

    // This implements the linear interpolation formula: result = bg * (1.0 - alpha) + fg * alpha
    // This can be factorized into: result = bg + (fg - bg) * alpha
    // alpha is in Q1.5 format, so 0.0 is represented by 0, and 1.0 is represented by 32
    uint32_t result = (fg - bg) * alpha; // parallel fixed-point multiply of all components
    result >>= 5;
    result += bg;
    result &= 0b00000111111000001111100000011111; // mask out fractional parts
    return (color)((result >> 16) | result); // contract result
}

*/


void sgl_draw_point(sgl_surf_t *surf, int16_t x, int16_t y, sgl_color_t color)
{
    sgl_color_t *pos = surf->fb;
    *(pos + y * surf->size.w + x) = color;
}

void sgl_draw_hline(sgl_surf_t *surf, int16_t x, int16_t y, int16_t len, sgl_color_t color)
{
    sgl_color_t *pos = surf->fb;
    pos += (y * surf->size.w + x);
    for(int i = 0; i < len; i++) {
        *(pos + i) = color;
    }
}

void sgl_draw_vline(sgl_surf_t *surf, int16_t x, int16_t y, int16_t len, sgl_color_t color)
{
    sgl_color_t *pos = surf->fb;
    pos += (y * surf->size.w + x);
    for(int i = 0; i < len; i++) {
        *(pos + i * surf->size.w) = color;
    }
}

void sgl_draw_area_color(sgl_surf_t *surf, int16_t x1, int16_t y1, int16_t x2, int16_t y2, const sgl_color_t color)
{
    sgl_color_t *pos = surf->fb;
    pos += (y1 * surf->size.w + x1);
    for(int i = 0; i < (y2 - y1); i ++) {
        for(int j = 0; j < (x2 - x1); j ++) {
            *(pos + i * surf->size.w + j) = color;
        }
    }
}

void sgl_draw_buffer_clear(sgl_surf_t *surf, sgl_color_t color)
{
#ifdef SGL_FULL_FRAMEBUFFER
    return;
#else
    sgl_color_t *pos = surf->fb;
    for(int i = 0 ;i < surf->size.w * surf->size.h; i++) {
        *pos = color;
        pos ++ ;
    }
#endif
}

void sgl_draw_obj_buffer_clear(sgl_surf_t *surf, sgl_obj_t *obj, sgl_color_t color)
{
#ifdef SGL_FULL_FRAMEBUFFER
    sgl_color_t *pos = surf->fb + obj->pos.y*surf->size.w + obj->pos.x;
    for(int i = 0 ;i < obj->size.h; i++) {
        for(int j = 0; j < obj->size.w; j++)
            *pos = color;
        pos += surf->size.w;
    }
#else
    sgl_color_t *pos = surf->fb;
    for(int i = 0 ;i < surf->size.w * surf->size.h; i++) {
        *pos = color;
        pos ++ ;
    }
#endif
}

void sgl_draw_buffer_flush(sgl_surf_t *surf, int16_t x, int16_t y)
{
    sgl_disp_area(x, y, x + surf->size.w, y + surf->size.h, surf->fb);
}

void sgl_draw_buffer_flush_hclip(sgl_surf_t *surf, int16_t x, int16_t y, int16_t clip_start, int16_t clip_end)
{
    sgl_disp_area(x, y, x + surf->size.w, y + (clip_end - clip_start), surf->fb + clip_start*surf->size.w);
}

void sgl_draw_buffer_flush_vclip(sgl_surf_t *surf, int16_t x, int16_t y, int16_t clip_start, int16_t clip_end)
{

}

//TODO: 是实现draw buffer的裁剪
void sgl_draw_area_flush(sgl_rect_t rect, sgl_color_t *src)
{
    sgl_disp_area(rect.x1, rect.y1, rect.x2, rect.y2, src);;
}

static sgl_surf_t sgl_active_surf;


sgl_surf_t* sgl_draw_get_active_surf(sgl_obj_t *obj)
{
#ifdef SGL_FULL_FRAMEBUFFER
    sgl_active_surf.fb = frame_buffer;
    sgl_active_surf.size.w = SGL_CONFIG_PANEL_XRES;
    sgl_active_surf.size.h = SGL_CONFIG_PANEL_YRES;
#else
    sgl_active_surf.fb = sgl_draw_buffer;
    sgl_active_surf.size = obj->size;
#endif
    return &sgl_active_surf;
}

void sgl_draw_set_active_surf(sgl_color_t *fb, int16_t width, int16_t height)
{
    sgl_active_surf.fb = fb;
    sgl_active_surf.size.w = width;
    sgl_active_surf.size.h = height;
}

void sgl_draw_set_active_surf_size(int16_t width, int16_t height)
{
    sgl_active_surf.size.w = width;
    sgl_active_surf.size.h = height;
}

/*  
 * This function is used to calculate circle path
 *  
 * @param cx: center x position of the circle 
 * @param cy: center y position of the circle 
 * @param r:  radius size of the circle 
 * @param path: path position of the circle
 * @return int: x and y middle
 * 
 * The algorithm adopts the DDA algorithm
 */
int sgl_figure_circle_path(int16_t r, int16_t* path)
{
    int32_t   deci;    /* Decision Variable */
    uint32_t  dx;      /* Current X Value   */
    uint32_t  dy;      /* Current Y Value   */
    deci = 3 - (r << 1);
    dx = 0;
    dy = r;
    while (dx <= dy)
    {
        path[dx] = dy;
        //path[dy] = dx;
        if (deci < 0) {
            deci += (dx << 2) + 6;
        }
        else {
            deci += ((dx - dy) << 2) + 10;
            dy--;
        }
        dx++;
    }
    return dx-1;
}

sgl_pos_t sgl_figure_arc_endpoint(int16_t r, int16_t deg)
{
    sgl_pos_t point = {-32768, -32768};
#if (SGL_CONFIG_DRAW_ALGO == 0)
    int mix = 0;
    int16_t path[SGL_CONFIG_DRAW_STACK_MAX];
    memset(path, 0, SGL_CONFIG_DRAW_STACK_MAX*2);
    sgl_figure_circle_path(r, path);
    int middle = sgl_figure_circle_path(r, path);
    for (int y = 0 ; y < r; y ++) {
        if( y <= middle) {
            //TODO: 
        }
    }
#else
    int f = 1 - r;
    int diameter = 2*r;
    int ddF_x = 0;
    int ddF_y = - diameter;
    int y = 0;
    int x = r;
    int16_t deg_tmp;
    //deg_tmp = sgl_fast_atan2(-r, 0);
    while(y < x) {
        if(f >= 0) {
            x--;
            ddF_y += 2;
            f += ddF_y;
        }
        y++;
        ddF_x += 2;

        deg_tmp = sgl_fast_atan2(-y, -x);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = -y;
            point.y = -x;
            return point;
        }
        deg_tmp = sgl_fast_atan2(-x, -y);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = -x;
            point.y = -y;
            return point;
        }
        deg_tmp = sgl_fast_atan2(y, -x);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = y;
            point.y = -x;
            return point;
        }
        deg_tmp = sgl_fast_atan2(x, -y);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = x;
            point.y = -y;
            return point;
        }
        deg_tmp = sgl_fast_atan2(-y, x);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = -y;
            point.y = x;
            return point;
        }
        deg_tmp = sgl_fast_atan2(-x, y);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = -x;
            point.y = y;
            return point;
        }
        deg_tmp = sgl_fast_atan2(y, x);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = y;
            point.y = x;
            return point;
        }
        deg_tmp = sgl_fast_atan2(x, y);
        printf("deg_tmp = %d\n", deg_tmp);
        if(deg == deg_tmp) {
            point.x = x;
            point.y = y;
            return point;
        }
        f += ddF_x + 1;
    }
    return point;
#endif
}

// int sgl_figure_circle_path(uint16_t cx, uint16_t cy, uint16_t r, uint16_t* path)
// {
//     int f = 1 - r;
//     int diameter = 2*r;
//     int ddF_x = 0;
//     int ddF_y = - diameter;
//     int y = 0;
//     int x = r;
//     while(y < x) {
//         path[x] = y;
//         path[y] = x;
//         if(f >= 0) {
//             x--;
//             ddF_y += 2;
//             f += ddF_y;
//         }
//         y++;
//         ddF_x += 2;
//         f += ddF_x + 1;
//     }
//     return x;
// }

int sgl_text_width(sgl_font_t *font, const char* text)
{
    int text_w = 0;
    while(*text) {
        text_w += font->table[(uint8_t)(*text) - 32].width;
        text ++;
    }
    return text_w;
}

int sgl_text_height(sgl_font_t *font)
{
    return font->font_height;
}

void sgl_draw_handler(sgl_obj_t *obj)
{
    switch(obj->obj_type) {
        case SGL_OBJ_BUTTON: {
            sgl_button_event_cb(obj);
            break;
        }
        case SGL_OBJ_SWITCH: {
            sgl_switch_event_cb(obj);
            break;
        }
        case SGL_OBJ_LABEL: {
            sgl_label_event_cb(obj);
            break;
        }
        case SGL_OBJ_BATTERY: {
            sgl_battery_draw(obj);
            break;
        }
        case SGL_OBJ_CHECKBOX: {
            sgl_checkbox_event_cb(obj);
            break;
        }
        case SGL_OBJ_RADIOBUTTON: {
            sgl_radiobtn_event_cb(obj);
            break;
        }
        case SGL_OBJ_NUMBER: {
            sgl_number_draw(obj);
            break;
        }
        case SGL_OBJ_PROGRESSBAR: {
            sgl_pgsbar_event_cb(obj);
            break;
        }
        default: break;
    }
}
